<?php
/**
 * Copyright (c) 2010, Slawomir Mazur <mazursllawek@gmail.com>
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * o Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * o Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * o Neither the name of the Softisland nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * Enter description here...
 *
 * @package     Admin
 * @subpackage  Admin_Network
 * @copyright   Slawomir Mazur (c) 2010 (http://www.zend.com/zce.php?c=ZEND011643)
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @author      Slawomir Mazur <mazursllawek@gmail.com>
 */

class Admin_NetworkController extends Zend_Controller_Action
{    
	public function indexAction()
    {
    	$this->view->collection = Gp_App::getModel('system/network')->getCollection();
    }
    
    public function addAction()
    {
    	$this->view->form = $this->_getForm();
    }
    
    public function editAction()
    {
    	$id = $this->getRequest()->getParam('id', null);
    	
    	try {
	    	if (!empty($id)) {
	    		throw new Exception('No network id specified!');
	    	}
	    	
	    	
	    	// view change
	    	$this->render('add');
	    	
    	} catch (Exception $e) {
    		$this->_helper->getHelper(Gp_View_Helper_Messenger::ADMIN_MESSENGER)
	    				->addError($e->getMessage());
	    	$this->_redirect('admin/netowrk');
    	}
    }
    
    public function editPostAction()
    {
    	$this->_helper->viewRenderer->setNoRender();
    	
    	if (!$this->getRequest()->isPost() || 
    			!$this->getRequest()->getParam('network', false)) { 
    		
    		$this->_redirect('admin/network/add');
    	}
    	
    	$form = $this->_getForm();
    	$data = $this->getRequest()->getParam('network', array());
    	
    	if ($form->isValid($data)) {
    		try {
	    		$id = (int) @$data['network_id'];
				$model = Gp_App::getModel('system/network');
				
				
				
    		} catch (Exception $e) {
    			$this->_helper->getHelper(Gp_View_Helper_Messenger::ADMIN_MESSENGER)
    					->addError($e->getMessage());
    			$this->_redirect('admin/network/add');
    		}
    		$this->_helper->getHelper(Gp_View_Helper_Messenger::ADMIN_MESSENGER)
    				->addSuccess('Network was generated successfully');
    		$this->_redirect('admin/network');
    	} else {
    		$allMessages = (array) $form->getMessages('network', true);
    		foreach ($allMessages as $filed => $messages) {
    			if (is_array($messages) && (count($messages) > 0)) {
    				foreach ($messages as $message) {
    					$this->_helper->getHelper(Gp_View_Helper_Messenger::ADMIN_MESSENGER)
    							->addError($message);
    				}
    			}
    		}
    		
    		$this->_redirect('admin/network/add');
    	}
    }
    
	protected function _getForm()
    {
    	$form = new Gp_Form();
		$form->setAction($this->view->baseUrl('admin/network/edit-post'))
			 ->setMethod('post')
			 ->setAttrib('id', 'network')
			 ->setElementsBelongTo('network')
			 ->setButtonsToReset(array('generate', 'cancel', 'delete'));
    	
        $networks = new Zend_Form_Element_Select('netowrk');
        $networks->setLabel('Networks')
        		->setAttrib('size', 15)
        		->addMultiOptions(
        			Gp_App::getModel('system/network')->getNetworksAsArray()
        		);		
        		
        $submit = new Zend_Form_Element_Button('generate');
        $submit->setAttrib('class', 'action')
        		->setAttrib('type', 'submit');
        
        $cancel = new Zend_Form_Element_Button('cancel');
        $cancel->setAttrib('class', 'action');
        
        $delete = new Zend_Form_Element_Button('delete');
        $delete->setAttrib('type', 'submit')
        		->setAttrib('hiddenElement', true);
        
		$form->addElements(array($networks, $submit, $cancel, $delete));
		
        return $form;
    }
}